package com.polaris.live.ui.party.fragment.base

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.View
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.ScreenUtils
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.widget.AnimUtils

/**
 * 实现了隐藏显示手势
 *
 * @author Created by 天晴 on 2024/3/7 18:08
 */
abstract class BasePartyGestureFragment<VM : BaseViewModel, VB : ViewBinding> : BasePartyFragment<VM, VB>() {

    private var mIsHideView = false // 是否是隐藏View
    private var mShowView: List<View> = mutableListOf()

    override fun initView(savedInstanceState: Bundle?) {
        mShowView = needShowViews()
    }

    fun onSwipeLeft() {
        withLoaded {
            if (!mIsHideView) {
                onHideView()
            }
        }
    }

    fun onSwipeRight() {
        withLoaded {
            if (mIsHideView) {
                onShowView()
            } else {
                showRightPopup()
            }
        }

    }

    private fun onShowView() {
        mIsHideView = false
        onHideView(mIsHideView)
        val width = ScreenUtils.getScreenWidth()
        playAnim(
            mShowView.map {
                inLeft(it, width)
            }.toList()
        )
    }

    private fun onHideView() {
        mIsHideView = true
        onHideView(mIsHideView)
        val width = ScreenUtils.getScreenWidth()
        playAnim(
            mShowView.map {
                outRight(it, width)
            }.toList()
        )
    }

    /**
     * 显示侧边栏
     */
    abstract fun showRightPopup()

    /**
     * 清屏数据组
     */
    abstract fun needShowViews(): List<View>

    /**
     * 是否清屏
     */
    abstract fun onHideView(pd: Boolean)

    fun playAnim(list: List<Animator>) {
        AnimUtils.run(mBinding.root, AnimatorSet().apply {
            duration = 350
            playTogether(list)
        })
    }

    fun inLeft(view: View, start: Int): Animator {
        val animator = AnimatorSet().apply {
            playTogether(
                ObjectAnimator.ofFloat(view, "translationX", start.toFloat(), 0f),
                ObjectAnimator.ofFloat(view, "alpha", 0f, 1f),
            )
        }
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationStart(animation: Animator) {
                view.visibility = ViewUtils.getViewVisible(view) ?: View.VISIBLE
            }
        })
        return animator
    }

    fun inAlpha(view: View): Animator {
        val animator = AnimatorSet().apply {
            playTogether(
                ObjectAnimator.ofFloat(view, "alpha", 0f, 1f),
            )
        }
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationStart(animation: Animator) {
                view.visibility = ViewUtils.getViewVisible(view) ?: View.VISIBLE
            }
        })
        return animator
    }

    fun outAlpha(view: View): Animator {
        val animator = AnimatorSet().apply {
            playTogether(
                ObjectAnimator.ofFloat(view, "alpha", 1f, 0f),
            )
        }
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationStart(animation: Animator) {
                view.visibility = ViewUtils.getViewVisible(view) ?: View.INVISIBLE
            }
        })
        return animator
    }

    fun outRight(view: View, end: Int, visibility: Int = View.INVISIBLE): Animator {
        val animator = AnimatorSet().apply {
            playTogether(
                ObjectAnimator.ofFloat(view, "translationX", 0f, end.toFloat()),
                ObjectAnimator.ofFloat(view, "alpha", 1f, 0f),
            )
        }
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                view.visibility = ViewUtils.getViewVisible(view) ?: visibility
            }
        })
        return animator
    }

}
